/*
 * Input.h
 *
 *	Author: stravant
 *	Description:
 *		Stores current key/button states
 */

#ifndef INPUT_H_
#define INPUT_H_

#include "../ui/Event.h"

#include <map>

namespace tbt {

class Input {
public:
	Input() {}
	~Input() {}

	void setKeyState(Key::Code c, bool st) {
		mKeyState[Key::ToGDKKeyCode(c)] = st;
		switch (c) {
		case Key::vkShift_L:
		case Key::vkShift_R:
			if (st) mFlags |= Event::kFlagKeyShift; else mFlags &= ~Event::kFlagKeyShift;
			break;
		case Key::vkControl_L:
		case Key::vkControl_R:
			if (st) mFlags |= Event::kFlagKeyCtrl; else mFlags &= ~Event::kFlagKeyCtrl;
			break;
		case Key::vkMenu:
		case Key::vkMenuKB:
		case Key::vkMenuPB:
			if (st) mFlags |= Event::kFlagKeyAlt; else mFlags &= ~Event::kFlagKeyAlt;
			break;
//		case Key::vkAlt_L:
//		case Key::
//			if (st) mFlags |= Event::kFlagKeyCmd; else mFlags &= ~Event::kFlagKeyCmd;
//			break;
		default:
			break;
		}
	}

	bool getKeyState(Key::Code c) const {
		return mKeyState[Key::ToGDKKeyCode(c)];
	}

	void setMousePos(const IVec& p) {
		mMousePos = p;
	}

	const IVec& getMousePos() const {
		return mMousePos;
	}

	void startDrag() {
		mDragStart = mMousePos;
	}

	const IVec& getDragStart() const {
		return mDragStart;
	}

	void setButtonState(Mouse::Button b, bool st) {
		mButtonState[b] = st;
		switch (b) {
		case Mouse::kButtonRight:
			if (st) mFlags |= Event::kFlagMouseRight; else mFlags &= ~Event::kFlagMouseRight;
			break;
		case Mouse::kButtonLeft:
			if (st) mFlags |= Event::kFlagMouseLeft; else mFlags &= ~Event::kFlagMouseLeft;
			break;
		case Mouse::kButtonCenter:
			if (st) mFlags |= Event::kFlagMouseCenter; else mFlags &= ~Event::kFlagMouseCenter;
			break;
		default:
			break;
		}
	}

	bool getButtonState(Mouse::Button b) const {
		return mButtonState[b];
	}

	Event::Flag getCurrentFlags() const {
		return static_cast<Event::Flag>(mFlags);
	}

private:
	bool mButtonState[Mouse::kButtonMax];
	mutable std::map<int, bool> mKeyState;
	IVec mMousePos;
	IVec mDragStart;
	//
	int mFlags;
};

}

#endif /* INPUT_H_ */
